package until;

import java.io.*;
import java.util.Properties;

/**
 * 文件工具类
 */

public class FileUtil {
    public static boolean createDir(File f){
        boolean b=false;
        if (!f.exists()){
            b=f.mkdirs();
        }else {
            b=true;
        }
        return b;
    }
    public static boolean createFile(File f){
        boolean b=false;
        try {
            if(!f.exists()) {
                b = f.createNewFile();
            }else {
                b=true;
            }
        }catch (IOException e){
           System.out.println("路径出错");
        }
        return b;
    }

    /**
     * 生成带副本的名称
     * @param
     * @return
     */

    public static String createFilename(File f){
        String fileName=f.getName();
       if(f.isDirectory()){
           return f.getName();
       }
       String newFileName="";
       int i=1;
       while (true){
           int index=fileName.lastIndexOf(".");
           String suffix=fileName.substring(index);
           String prefix=fileName.substring(0,index-1);//
           if (i == 1) {
               prefix+="- 副本";
           }else {
               prefix+="- 副本("+i+")";
           }

           newFileName=prefix+suffix;
           File file=new File(f.getParent(),newFileName);
           if(file.exists()){
               i++;
               continue;
           }
           break;
       }

       return newFileName;
    }

    /**
     * 字节流
     * @param in
     * @param out
     */
    public  static void fileInputAndOutput(File in,File out){
        InputStream is=null;
        OutputStream os=null;
        try{
            is=new FileInputStream(in);
            os=new FileOutputStream(out,false);

            int i=0;
            byte bs[]=new byte[1024];
            while ((i=is.read(bs))!=-1){
                os.write(bs,0,i);
            }

            if(os!=null)os.close();
            if(is!=null)is.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 字符流
     * @param reader
     * @param writer
     */
    public static void fileReadAndWriter(File reader,File writer){
        FileReader fr=null ;
        BufferedReader br=null;
        FileWriter fw=null;
        BufferedWriter bw=null;
        try {
            fr=new FileReader(reader);
            br=new BufferedReader(fr);
            fw=new FileWriter(writer,false);
            bw=new BufferedWriter(fw);
            String $s="";
            while (($s=br.readLine())!=null){
                  bw.write($s);
            }
            bw.flush();
            if(bw!=null)bw.close();
            if(fw!=null)fw.close();
            if(br!=null)br.close();
            if(fr!=null)fr.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public  static  void outputObject(File f,Object obj){
        OutputStream os=null;
        ObjectOutputStream oos=null;
        try {
            os=new FileOutputStream(f,false);
            oos=new ObjectOutputStream(os);
            oos.writeObject(obj);
            if(oos!=null)oos.close();
            if(os!=null)os.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static Object inputObject(File f){

        try {
            InputStream is=new FileInputStream(f);
            ObjectInputStream ois=new ObjectInputStream(is);
            Object obj=ois.readObject();
            if(ois!=null)ois.close();
            if(is!=null)is.close();
            return obj;
        }catch (EOFException e){
            System.out.println("第一次读取对象为空");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 输出属性内容
     * @param f
     * @param pro
     */
    public  static void storeProperties(File f, Properties pro){
        try {
            OutputStream os=new FileOutputStream(f,false);
            pro.store(os,null);
            if(os!=null) os.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 读取属性文件
     */
    public static  Properties  loadPro(File f){
        try {
            InputStream is=new FileInputStream(f);
            Properties pro=new Properties();
            pro.load(is);
            if (is!=null)is.close();
            return pro;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
